<?
class mssql extends errorHandler{
	public $connected=false;
	public $result=null;
	public $affectedRows=0;
	public $rows;
	public $row;
	public $eof=true;
	public $insertId=0;
	public $index=0;
	public $rowCount=0;
	public $cacheTimeout=0;
	public $proc=NULL;
	public $params=NULL;
	public $db=NULL;

	public $config=NULL;

	public function __construct($config){
		parent::__construct();
		$this->config=$config;
		$this->cacheTimeout=DB_CACHE;
		$this->params=array();
	}

	public function setStoredProcedure($value){
		$this->proc=$value;
	}

	public function addParameter($param,$value,$dataType,$isOutput=false,$isNull=false,$varLen=NULL){
		$this->params[$param]=array(
			"value"=>$value,
			"dataType"=>$dataType,
			"isOutput"=>$isOutput,
			"isNull"=>$isNull,
			"varLen"=>$varLen
		);
	}

	public function connect($db=NULL){
		$result=false;
		if($this->connected){
			$result=true;
		}else{
			$host=$this->config["host"];
			$un=$this->config["user"];
			$pw=$this->config["password"];
			$db=$this->config["database"];
			$this->link=mssql_connect($host,$un,$pw);
			if($this->link){
				$isDbSelectError=false;
				mssql_select_db($db,$this->link) or $isDbSelectError=true;
				if($isDbSelectError||!$this->link||is_null($this->link)){
					$this->addError($this->ERROR_DB_SELECT,"msdatabase.connect.databaseCantSelect",array("mssql_error"=>mssql_get_last_message(),"db"=>$db,"un"=>$un,"pw"=>$pw,"host"=>$host));
				}else{
					$this->debug("MSSQL Database Connected",$db);
					$this->debug("MSSQL Link",$this->link);
					$result=true;
					$this->connected=true;
				}
			}else{
				$this->addError($this->ERROR_DB_LINK,"msdatabase.connect.databaseCantConnect",array("mssql_error"=>mssql_get_last_message(),"db"=>$db,"un"=>$un,"pw"=>$pw,"host"=>$host));
			}
		}
		return($result);
	}
	
	public function exec($sQl=NULL,$saveResultArray=false,$errorOnNoAffectedRows=false){
		$result=false;
		$row_count=0;
		if($this->connected){
			ini_set('odbc.defaultlrl','65536');
			ini_set('mssql.textlimit','2147483647');
			ini_set('mssql.textsize','2147483647'); 
			mssql_query('SET TEXTSIZE 2147483647',$this->link);
			$this->result=mssql_query($sQl,$this->link);
			if(!$this->result){
				$this->debug("msdatabase Error",$sQl);
				$this->addError($this->ERROR_DB_QUERY,"msdatabase.exec.queryError",array("mssql_error"=>mssql_get_last_message(),"sQl"=>$sQl));
			}elseif(strlen($this->proc)>0){
				$stmt=mssql_init($this->proc);
				foreach($this->params as $key=>$vals){
					mssql_bind($stmt,$key,$vals["value"],$vals["dataType"],$vals["isOutput"],$vals["isNull"],$vals["varLen"]);
				}
				mssql_execute($stmt);
				mssql_free_statement($stmt);
				$this->proc=NULL;
				$this->params=array();
			}else{
				$this->rows=array();
				$this->row=mssql_fetch_assoc($this->result);
				array_push($this->rows,$this->row);
				$this->index=0;
				$c=1;
				if($saveResultArray){
					while($ar=mssql_fetch_assoc($this->result)){
						array_push($this->rows,$ar);
						$c++;
					}
				}
				$this->eof=($c>0)?false:true;
				$result=($c>0);
				mssql_free_result($this->result);
			}
		}else{
			$this->addError($this->ERROR_DB_CONNECTION,"msdatabase.exec.databaseNotConnected",array("mssql_error"=>mssql_get_last_message(),"sQl"=>$sQl));
		}
		return($result);
	}
	
	public function moveNext(){
		$result=false;
		if($ar=mssql_fetch_assoc($this->result)){
			$this->row=$ar;
			array_push($this->rows,$ar);
			$this->index++;
			$result=true;
			if($this->index>=$this->affectedRows){
				$this->eof=true;
			}
		}else{
			$this->eof=true;
		}
		return($result);
	}

	public function escape($str){
		$result=stripslashes($str);
		$result="'".preg_replace("/'/","''",$result)."'";
		return($result);
	}

	public function close(){
		if($this->connected&&is_resource($this->link)){
			$this->connected=false;
			mssql_close($this->link);
		}
		return(true);
	}
}
?>